package cn.jji8.patternLogin.spigot.login;

import cn.jja8.patronSaint_2021_7_8_2056.bukkit.command.CommandManger;
import cn.jja8.patronSaint_2021_7_8_2056.bukkit.config.ConfigManger;
import cn.jja8.patronSaint_2021_7_8_2056.bukkit.config.IntSet;
import cn.jja8.patronSaint_2021_7_8_2056.bukkit.vision.StraightLine;
import cn.jji8.patternLogin.spigot.data.Data;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.util.ArrayList;

public class Configuring {
    //面板位置
    static int LoginLocationX,LoginLocationY,LoginLocationZ;
    //插件消息前缀
    static String newsPrefix = "[patternLogin]:";
    //登录位置
    static int playerLoginX,playerLoginY,playerLoginZ,playerR;
    //第一次安装
    static boolean first = false;
    //登录成功返回上传下线时位置
    static boolean logician = true;
    //设置登录后还在登录世界就传送到设置的世界
    public static boolean loginStillLoanword = true;
    static String loginTPotherWord = "world";
    //粒子效果
    public static int time = 5,density = 10;
    public static Particle effect = Particle.SOUL_FIRE_FLAME;

    public static void load(Plugin plugin){
        //--------------------------------------------------------------------命令 图案登录配置 config.yml
        {
            ArrayList<String> arrayList = new ArrayList<>();
            arrayList.add("图案登录配置");
            CommandManger commandManger = new CommandManger(plugin,"patternLoginConfig",arrayList,"patternLogin.admin");
            ConfigManger configManger = new ConfigManger(new File(plugin.getDataFolder(),"config.yml"));
            configManger.load("LoginLocation.LoginWord", "patternLoginWorld", s -> LoginWorld.wordName = s);
            configManger.load("LoginLocation.X", 0, (IntSet) i -> LoginLocationX = i);
            configManger.load("LoginLocation.Y", 0, (IntSet) i -> LoginLocationY = i);
            configManger.load("LoginLocation.Z", 4, (IntSet) i -> LoginLocationZ = i);
            configManger.load("playerLogin.X", 2, (IntSet) i -> playerLoginX = i);
            configManger.load("playerLogin.Y", 1, (IntSet) i -> playerLoginY = i);
            configManger.load("playerLogin.Z", 3, (IntSet) i -> playerLoginZ = i);
            configManger.load("newsPrefix", "[patternLogin]:", i -> newsPrefix = i);
            commandManger.setMessagePromptPrefix(newsPrefix);
            configManger.load("first", true, aBoolean -> {
                if(aBoolean){
                    first=true;
                    configManger.set("first",false);
                }
            });
            configManger.load("密码-设置密码错误指定次数触发等待时间", 5,commandManger, (IntSet) i -> PlayerLogin.maxPasswordError = i);
            configManger.load("密码-设置密码错误等待时间", 60,commandManger, (IntSet) i -> PlayerLogin.errorTime = i);
            configManger.load("登录时-玩家登录最大移动半径", 1,commandManger, (IntSet) i -> playerR = i);
            configManger.load("登录后-登录后返回上次下线时的位置", true,commandManger, i -> logician = i);
            configManger.load("登录后-设置登录后还在登录世界就传送到的世界", "world",commandManger, i -> loginTPotherWord = i);
            configManger.load("登录后-设置登录后还在登录世界就传送到设置的世界", true,commandManger, i -> loginStillLoanword = i);
            configManger.load("粒子效果-显示间隔Tick", 5,commandManger, (IntSet) i -> time = i);
            configManger.load("粒子效果-显示密度", 10,commandManger, (IntSet) i -> density = i);
            configManger.load("粒子效果-显示效果", "SOUL_FIRE_FLAME",commandManger, i -> {
                try {
                    effect = Particle.valueOf(i);
                }catch (IllegalArgumentException e){
                    effect = Particle.FLAME;
                    plugin.getLogger().warning("'"+i+"'不是一个有效的粒子效果！");
                }
            });
            commandManger.addCommand(new String[]{"登录时-设置登录面板位置为当前位置"}, (commandSender, strings) -> {
                if(!(commandSender instanceof Player)){
                    commandSender.sendMessage(newsPrefix+"这个命令必须玩家执行");
                    return;
                }
                Location location =((Player)commandSender).getLocation().getBlock().getLocation();
                World world = location.getWorld();
                if(world==null){
                    commandSender.sendMessage(newsPrefix+"你所在的世界不是一个有效的世界。");
                    return;
                }
                configManger.set("LoginLocation.LoginWord",world.getName());
                configManger.set("LoginLocation.X",location.getBlockX());
                configManger.set("LoginLocation.Y",location.getBlockY());
                configManger.set("LoginLocation.Z",location.getBlockZ());
                LoginWorld.BuildingPlatform(location);
                LoginWorld.loginWord = location.getWorld();
                commandSender.sendMessage(newsPrefix+"设置完成");
            });
            commandManger.addCommand(new String[]{"登录时-设置玩家登录位置为当前位置"}, (commandSender, strings) -> {
                if(!(commandSender instanceof Player)){
                    commandSender.sendMessage(newsPrefix+"这个命令必须玩家执行");
                    return;
                }
                Location location =((Player)commandSender).getLocation();
                configManger.set("playerLogin.X",location.getBlockX());
                configManger.set("playerLogin.Y",location.getBlockY());
                configManger.set("playerLogin.Z",location.getBlockZ());
                commandSender.sendMessage(newsPrefix+"设置完成");
            });
        }{
            //-----------------------------------------------------------------------------------------------------------------------------
            ArrayList<String> arrayList = new ArrayList<>();
            arrayList.add("图案登录管理");
            CommandManger commandManger = new CommandManger(plugin,"patternLoginAdmin",arrayList,"patternLogin.admin");
            commandManger.setMessagePromptPrefix(newsPrefix);
            commandManger.addCommand(new String[]{"清除某玩家密码"}, (commandSender, strings) -> {
                if(strings.length<1){
                    commandSender.sendMessage(newsPrefix+"缺少玩家名参数。");
                    return;
                }
                String password = Data.getPassword(strings[0]);
                if(password==null){
                    commandSender.sendMessage(newsPrefix+strings[0]+"玩家并没有注册。");
                    return;
                }else {
                    Data.setPassword(strings[0],"0");
                }
                Player player = Bukkit.getPlayer(strings[0]);
                if(player!=null){
                    LoginManager.setPlayerNeedLogin(player);
                }
            },new String[][]{{"玩家名"}});
        }
    }
}
